package team.seekers.oj.homework.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.oj.auth.enums.PermissionEnum;
import team.seekers.oj.common.entity.UserSessionDTO;
import team.seekers.oj.common.enums.ApiExceptionEnum;
import team.seekers.oj.common.exception.ApiException;
import team.seekers.oj.common.util.AssertUtils;
import team.seekers.oj.common.util.RedisUtils;
import team.seekers.oj.contest.client.ProblemClient;
import team.seekers.oj.contest.client.SubmissionClient;
import team.seekers.oj.contest.client.UserClient;
import team.seekers.oj.contest.entity.ContestDO;
import team.seekers.oj.homework.conventer.HomeworkConverter;
import team.seekers.oj.homework.conventer.HomeworkCreateReqConverter;
import team.seekers.oj.homework.conventer.HomeworkListConverter;
import team.seekers.oj.homework.conventer.HomeworkManageConverter;
import team.seekers.oj.homework.dao.HomeworkDao;
import team.seekers.oj.homework.dao.HomeworkListDao;
import team.seekers.oj.homework.dto.HomeworkCreateReqDTO;
import team.seekers.oj.homework.dto.HomeworkManageDTO;
import team.seekers.oj.homework.entity.HomeworkDO;

import java.util.Date;

@Slf4j
@Service
public class HomeworkManageService {

    @Autowired
    private HomeworkDao homeworkDao;

    @Autowired
    private HomeworkListDao homeworkListDao;

    @Autowired
    private HomeworkConverter homeworkConverter;

    @Autowired
    private HomeworkCreateReqConverter homeworkCreateReqConverter;

    @Autowired
    private HomeworkManageConverter homeworkManageConverter;

    @Autowired
    private HomeworkListConverter homeworkListConverter;

    @Autowired
    private ProblemClient problemClient;

    @Autowired
    private SubmissionClient submissionClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private RedisUtils redisUtils;


    public Long create(HomeworkCreateReqDTO reqDTO) {
        HomeworkDO homeworkDO = homeworkCreateReqConverter.from(reqDTO);
        homeworkDao.save(homeworkDO);
        return homeworkDO.getId();

    }

    public boolean isHomeworkManager(Long homeworkId, UserSessionDTO userSessionDTO) {

        return PermissionEnum.SUPERADMIN.in(userSessionDTO) ||
                userSessionDTO.userIdEquals(homeworkId);
    }

    public HomeworkManageDTO query(Long homeworkId) {
        HomeworkDO homeworkDO = homeworkDao.lambdaQuery().select(
                HomeworkDO::getId,
                HomeworkDO::getGmtStart,
                HomeworkDO::getGmtEnd,
                HomeworkDO::getUserId,
                HomeworkDO::getProblems,
                HomeworkDO::getParticipantGroups
        ).eq(HomeworkDO::getId, homeworkId).one();
        AssertUtils.notNull(homeworkDO, ApiExceptionEnum.CONTEST_NOT_FOUND);
        return homeworkManageConverter.to(homeworkDO);

    }

    public void update(HomeworkManageDTO reqDTO) {
        HomeworkDO homeworkDO = homeworkManageConverter.from(reqDTO);
        homeworkDao.updateById(homeworkDO);
    }
}